// window.rbacVues =
;
(function() {
  new Vue({
    el: "#exclusive_service_create",
    data() {
      return {
        index: 0,
        pid: 0,
        memberType: 'A',
        isBindalert: false,
        memberData: [],
        odata: [],
        adata: [],
        modelList: [],
        csid: null,
        userList: [],
        responsible_area: '',
        responsible_user_name: '',
        responsible_user_mobile: '',
        responsible_user_wechat:null,
        permissions: JSON.parse(window.localStorage.getItem('permissions')),
        orgItemList: [],
        entryType: '', //编辑还是新增
        organizations: [], //编辑时已选组织
      }
    },
    computed: {
      treeList() {
        let arr = [];
        let parent, children;
        parent = this.adata
        children = this.adata
        for (let i = parent.length - 1; i >= 0; i--) {
          let item0 = parent[i];
          for (let j = 0; j < children.length; j++) {
            let item1 = children[j];
            if (item0.id == item1.parentId) {
              item0.children.push(item1)
            }
          }
        }
        for (let k = 0; k < parent.length; k++) {
          if (Number(parent[k].parentId) > Number(this.csid)) {
            parent.splice(k, 1)
            k--
          }
        }
        arr = parent
        let count = 0;
        guierzi(arr, count)
        return arr
      }
    },
    created() {
      this.entryType = hlUrlUtil.GetQueryString('type')
      if (this.entryType == 'edit') {
        this.getItemService();
      }
      this.getStwxdd();
    },
    methods: {
      // 获取服务详情
      getItemService() {
        hlhttp.get({
          url: 'jwk/exclusive_service',
          data: {
            exclusive_service_id: hlUrlUtil.GetQueryString('id')
          },
          success: (res) => {
            this.responsible_area = res.responsibleArea
            this.responsible_user_name = res.responsibleUserName
            this.responsible_user_mobile = res.responsibleUserMobile
            this.responsible_user_wechat = res.responsibleUserWechat

            res.organizations.forEach(item => {
              item.memberOrgId = item.organizationId
              item.shortName = item.organizationName
              item.select = true
            })
            this.organizations = res.organizations
            this.orgItemList = JSON.parse(JSON.stringify(res.organizations))
            this.showSelectOrg()
          }
        })
      },
      // 编辑时显示已选组织
      showSelectOrg() {
        if (this.orgItemList.length) {
          this.orgItemList.forEach(item => {
            this.odata.some(single => {
              if (item.organizationId == single.memberOrgId) {
                single.select = true
              }
            })
          })
        }
        this.adata = JSON.parse(JSON.stringify(this.odata))

      },
      //获取身份信息
      getStwxdd() {
        hlhttp.get({
          url: 'user_sub_organizations',
          data: {
            pid: this.pid,
            member_type: this.memberType,
            show_member: 'N'
          },
          success: (res) => {
            if (!res.subOrgs.length) return;
            res.subOrgs.some(item => {
              if (item.orgType == 'DD') {
                this.pid = item.id
                this.csid = item.id
                this.memberData = res.subOrgs;
                this.getStwxddData()
              }
            })
          }
        })

      },
      //根据身份信息id调用
      getStwxddData(fn) {
        hlhttp.get({
          url: 'user_sub_organizations',
          data: {
            pid: this.pid,
            member_type: this.memberType,
            show_member: 'N'
          },
          success: (data) => {
            if (data.length == 0) {
              $(".addarea").css('display', 'block')
            }

            data.subOrgs.map(item => {
              item.flag = true;
              item.oflag = false;
              item.select = false;
              item.parentId = this.pid;
              item.children = [];
              this.odata.push(item)
            })
            this.odata = hlArrayUtil.removeRepeat(this.odata, 'id');
            this.showSelectOrg();
            if (fn) fn();
          }
        })
      },
      getPidData(id) {
        this.pid = id;
        this.getStwxddData()
      },
      //组织筛选项
      selectOrg() {
        this.isBindalert = true
        this.showSelectOrg();
      },
      // 获取选择的组织
      getdata(id, obj) {
        this.odata.some(item => {
          if (item.memberOrgId == id) {
            item.select = !item.select
          }
        })

        this.adata = JSON.parse(JSON.stringify(this.odata))

        if (!obj.select) {
          this.orgItemList.push(obj)
        } else {
          this.orgItemList.some((item, index) => {
            if (item.memberOrgId == id) {
              this.orgItemList.splice(index, 1)
            }
          })
        }
      },
      // 确认添加/编辑
      sure_add_edit() {
        let organization_ids = []
        this.orgItemList.forEach(item => {
          organization_ids.push(item.memberOrgId)
        })
        organization_ids = organization_ids.join(',')
        if (this.entryType == 'add') {
          hlhttp.post({
            url: 'jwk/exclusive_service',
            data: {
              'responsible_area': this.responsible_area,
              'responsible_user_name': this.responsible_user_name,
              'responsible_user_mobile': this.responsible_user_mobile,
              'responsible_user_wechat':this.responsible_user_wechat,
              'organization_ids': organization_ids,
            },
            success: (data) => {
              hlUrlUtil.goTo('jwkExclusiveService',{'ddtab':true})
            }
          })
        }
        if (this.entryType == 'edit') {
          hlhttp.put({
            url: 'jwk/exclusive_service',
            data: {
              'exclusive_service_id': hlUrlUtil.GetQueryString('id'),
              'responsible_area': this.responsible_area,
              'responsible_user_name': this.responsible_user_name,
              'responsible_user_mobile': this.responsible_user_mobile,
              'responsible_user_wechat':this.responsible_user_wechat,
              'organization_ids': organization_ids,
            },
            success: (data) => {
              hlUrlUtil.goTo('jwkExclusiveService',{'ddtab':true})
            }
          })
        }

      },
      //检查是否填写完整
      checkMes() {
        if (!this.responsible_area) {
          hl.toast('请输入负责地区')
          return
        }
        if (!this.responsible_user_name) {
          hl.toast('请输入负责人')
          return
        }
        if (!this.responsible_user_mobile) {
          hl.toast('请输入电话')
          return
        }
        if (!this.userList.length) {
          hl.toast('请选择组织')
          return
        }
      },
      //减少右边
      reduce(id) {
        this.odata.some(item => {
          if (item.memberOrgId == id) {
            item.select = !item.select
          }
        })

        this.orgItemList.some((item, index) => {
          if (id == item.memberOrgId) {
            this.orgItemList.splice(index, 1)
          }
        })
        this.adata = JSON.parse(JSON.stringify(this.odata))
      },
      //确定选择组织
      sureselectOrg() {
        this.organizations = JSON.parse(JSON.stringify(this.orgItemList))
        this.isBindalert = false
      },
      cancle() {
        this.isBindalert = false
      }

    },
  })

  function guierzi(arr, count) {
    count++
    arr.map(item => {
      item.level = count
      if (item.children.length) {
        guierzi(item.children, count)
      }
    })
  }
}())
